<?php

namespace App\Utils\sms;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class AliSms implements BaseSms
{
    public static function send($phone, $code){
        AlibabaCloud::accessKeyClient(config('sms.appid'), config('sms.token'))
            ->regionId('cn-hangzhou')
            ->asDefaultClient();

        try {
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host('dysmsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => $phone,
                        'SignName' => config('sms.sms_sig'),
                        'TemplateCode' => config('sms.sms_tpl'),
                        'TemplateParam' => "{'code':$code}",
                    ],
                ])
                ->request();
//            print_r($result->toArray());
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }
        $res = $result->toArray();

        if($res['Message']=='OK'){
            return true;
        }else{
            return false;
        }


    }
}



